{
 "metadata": {},
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Matplotlib: scrolling plot\n",
      "======================================================================\n",
      "\n",
      "Controlling an Embedded Plot with wx Scrollbars\n",
      "-----------------------------------------------\n",
      "\n",
      "When plotting a very long sequence in a matplotlib canvas embedded in a\n",
      "wxPython application, it sometimes is useful to be able to display a\n",
      "portion of the sequence without resorting to a scrollable window so that\n",
      "both axes remain visible. Here is how to do so:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from numpy import arange, sin, pi, float, size\n",
      "\n",
      "import matplotlib\n",
      "matplotlib.use('WXAgg')\n",
      "from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg\n",
      "from matplotlib.figure import Figure\n",
      "\n",
      "import wx\n",
      "\n",
      "class MyFrame(wx.Frame):\n",
      "    def __init__(self, parent, id):\n",
      "        wx.Frame.__init__(self,parent, id, 'scrollable plot',\n",
      "                style=wx.DEFAULT_FRAME_STYLE ^ wx.RESIZE_BORDER,\n",
      "                size=(800, 400))\n",
      "        self.panel = wx.Panel(self, -1)\n",
      "\n",
      "        self.fig = Figure((5, 4), 75)\n",
      "        self.canvas = FigureCanvasWxAgg(self.panel, -1, self.fig)\n",
      "        self.scroll_range = 400\n",
      "        self.canvas.SetScrollbar(wx.HORIZONTAL, 0, 5, \n",
      "                                 self.scroll_range)\n",
      "        \n",
      "        sizer = wx.BoxSizer(wx.VERTICAL)\n",
      "        sizer.Add(self.canvas, -1, wx.EXPAND)\n",
      "\n",
      "        self.panel.SetSizer(sizer)\n",
      "        self.panel.Fit()\n",
      "\n",
      "        self.init_data()\n",
      "        self.init_plot()\n",
      "\n",
      "        self.canvas.Bind(wx.EVT_SCROLLWIN, self.OnScrollEvt)\n",
      "\n",
      "    def init_data(self):\n",
      "        \n",
      "        # Generate some data to plot:\n",
      "        self.dt = 0.01\n",
      "        self.t = arange(0,5,self.dt)\n",
      "        self.x = sin(2*pi*self.t)\n",
      "\n",
      "        # Extents of data sequence: \n",
      "        self.i_min = 0\n",
      "        self.i_max = len(self.t)\n",
      "\n",
      "        # Size of plot window:       \n",
      "        self.i_window = 100\n",
      "\n",
      "        # Indices of data interval to be plotted:\n",
      "        self.i_start = 0\n",
      "        self.i_end = self.i_start + self.i_window\n",
      "\n",
      "    def init_plot(self):\n",
      "        self.axes = self.fig.add_subplot(111)\n",
      "        self.plot_data = \\\n",
      "                  self.axes.plot(self.t[self.i_start:self.i_end],\n",
      "                                 self.x[self.i_start:self.i_end])[0]\n",
      "\n",
      "    def draw_plot(self):\n",
      "\n",
      "        # Update data in plot:\n",
      "        self.plot_data.set_xdata(self.t[self.i_start:self.i_end])\n",
      "        self.plot_data.set_ydata(self.x[self.i_start:self.i_end])\n",
      "\n",
      "        # Adjust plot limits:\n",
      "        self.axes.set_xlim((min(self.t[self.i_start:self.i_end]),\n",
      "                           max(self.t[self.i_start:self.i_end])))\n",
      "        self.axes.set_ylim((min(self.x[self.i_start:self.i_end]),\n",
      "                            max(self.x[self.i_start:self.i_end])))\n",
      "\n",
      "        # Redraw:                  \n",
      "        self.canvas.draw()\n",
      "\n",
      "    def OnScrollEvt(self, event):\n",
      "\n",
      "\t# Update the indices of the plot:\n",
      "        self.i_start = self.i_min + event.GetPosition()\n",
      "        self.i_end = self.i_min + self.i_window + event.GetPosition()\n",
      "        self.draw_plot()\n",
      "\n",
      "class MyApp(wx.App):\n",
      "    def OnInit(self):\n",
      "        self.frame = MyFrame(parent=None,id=-1)\n",
      "        self.frame.Show()\n",
      "        self.SetTopWindow(self.frame)\n",
      "        return True\n",
      "\n",
      "if __name__ == '__main__':\n",
      "    app = MyApp()\n",
      "    app.MainLoop()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}